package com.ypinkj.app.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ypinkj.app.entity.Dict;
import com.ypinkj.app.entity.SysArea;
import com.ypinkj.app.entity.TreeDto;
import com.ypinkj.app.entity.User;
import com.ypinkj.app.mapper.SysAreaDao;
import com.ypinkj.app.mapper.UserMapper;
import com.ypinkj.app.service.SysAreaService;
import com.ypinkj.app.utils.TreeToolUtils;
import com.ypinkj.app.utils.resultutils.Result;
import com.ypinkj.app.utils.resultutils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "区域")
@RestController
@RequestMapping("/sysAreas")
public class SysAreaController {

    @Autowired
    private SysAreaDao sysAreaDao;


    @Autowired
    private SysAreaService sysAreaService;

/*    @Autowired
    private UserMapper userMapper;*/

    @PostMapping
    @ApiOperation(value = "保存")
    public SysArea save(@RequestBody SysArea sysArea) {
        sysAreaDao.save(sysArea);
        return sysArea;
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取")
    public SysArea get(@PathVariable String id) {
        return sysAreaDao.getById(id);
    }

    @PutMapping
    @ApiOperation(value = "修改")
    public SysArea update(@RequestBody SysArea sysArea) {
        sysAreaDao.update(sysArea);
        return sysArea;
    }

    @GetMapping
    @ApiOperation(value = "列表")
    public List<SysArea> list() {
        QueryWrapper<SysArea> wrapper = new QueryWrapper<SysArea>();
        //wrapper.like("username","1");
        List<SysArea> areaList = sysAreaDao.selectList(wrapper);
        return areaList;
    }

    @ApiOperation(value = "获取分页")
    @GetMapping("/getPageHelper")
    public PageInfo TestPageHelper() {
        QueryWrapper<SysArea> wrapper = new QueryWrapper<SysArea>();
        //wrapper.like("username","1");
        List<SysArea> areaList = sysAreaDao.selectList(wrapper);
        Page<Object> pageObject = PageHelper.startPage(1, 10);
        PageInfo<SysArea> info = new PageInfo<>(areaList);
        System.out.println(info);
        return info;
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public void delete(@PathVariable String id) {
        sysAreaDao.delete(id);
    }


    /**
     * 区域三级联动 省市区
     */
    @RequestMapping("/getTreeArea")
    public Result getTreeArea() {

        ArrayList<TreeDto> rootList = new ArrayList<>();//根节点
        ArrayList<TreeDto> bodyList = new ArrayList<>();//子节点


        /**
         * 获取List
         */
        List<SysArea> areas = sysAreaService.list();


        for (int i = 0; i < areas.size(); i++) {

            if (areas.get(i).getParent_id().equals("1")) {
                TreeDto treeDto = new TreeDto(areas.get(i).getId().toString(), areas.get(i).getName(), areas.get(i).getParent_id().toString(), "true", null);
                rootList.add(treeDto);
            } else if(!areas.get(i).getParent_id().equals("0")){

                TreeDto treeDto = new TreeDto(
                        areas.get(i).getId().toString(),
                        areas.get(i).getName(),
                        areas.get(i).getParent_id().toString(),
                        "true", null
                );
                bodyList.add(treeDto);


            }
        }
        TreeToolUtils utils = new TreeToolUtils(rootList, bodyList);

        List<TreeDto> result =  utils.getTree();

        return  ResultUtil.success(result);

    }


}
